package com.xinyi.reactivemvvm.ui.activity;

import android.view.View;
import androidx.lifecycle.ViewModelProvider;

import com.xinyi.basic.utils.LogUtil;
import com.xinyi.reactivemvvm.R;
import com.xinyi.reactivemvvm.basic.activity.BaseViewBindingActivity;
import com.xinyi.reactivemvvm.databinding.ActivityMainBinding;
import com.xinyi.reactivemvvm.model.NeteaseCommentModel;
import com.xinyi.reactivemvvm.model.Resource;
import com.xinyi.reactivemvvm.model.request.NeteaseCommentRequest;
import com.xinyi.reactivemvvm.viewmodel.MainViewModel;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * @author 杨耿雷
 * @date 2024/9/23 22:44
 * @description 应用主页
 */
@AndroidEntryPoint
public class MainActivity extends BaseViewBindingActivity<ActivityMainBinding> implements View.OnClickListener {

    /**
     * 主页ViewModel
     */
    private MainViewModel mMainViewModel;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initObserveUI() {
        super.initObserveUI();
        mMainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        mMainViewModel.getNeteaseComment().observe(this, comment -> {
            if (comment instanceof Resource.Loading) {
                // 加载中
                binding.tvIvCommentTxt.setText(getString(R.string.loading));
            } else if (comment instanceof Resource.Success) {
                NeteaseCommentModel neteaseComment = ((Resource.Success<NeteaseCommentModel>) comment).getValue();
                binding.tvIvCommentTxt.setText(neteaseComment.getContent());
            } else if (comment instanceof Resource.Fail) {
                // 加载失败
                LogUtil.e(((Resource.Fail<NeteaseCommentModel>) comment).getException());
            }
        });
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        binding.tvRefresh.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_refresh) {
            NeteaseCommentRequest request = new NeteaseCommentRequest();
            request.setFormat("json");
            mMainViewModel.requestNeteaseComment(request);
        }
    }
}